<?php

namespace app\company\controller\consult\subset;

use app\admin\service\ExportBlackService;
use app\BaseController;
use app\model\ConsultBlacklistModel;
use app\model\UnitModel;
use app\model\UserModel;
use think\App;

class BlackList extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 排班间隔时段选择器
     * @return bool|string
     */
    public static function SchedulingDuration(): bool|string
    {
        $data = [
            ['value' => 5, 'name' => '5分钟'],
            ['value' => 10, 'name' => '10分钟'],
            ['value' => 15, 'name' => '15分钟'],
            ['value' => 20, 'name' => '20分钟'],
            ['value' => 30, 'name' => '30分钟'],
            ['value' => 60, 'name' => '60分钟'],
        ];
        return success($data);
    }

    /**
     * 拉黑时长选择器
     * @return bool|string
     */
    public function blackoutDuration(): bool|string
    {
        $data = [
            ['time' => 3 * 24 * 60 * 60, 'day' => 3, 'name' => '三天'],
            ['time' => 7 * 24 * 60 * 60, 'day' => 7, 'name' => '七天'],
            ['time' => 30 * 24 * 60 * 60, 'day' => 30, 'name' => '三十天'],
            ['time' => 0, 'day' => 0, 'name' => '永久'],
        ];
        return success($data);
    }

    /**
     * 拉黑原因选择器
     * @return bool|string
     */
    public function blackReason(): bool|string
    {
        $data = [
            ['key' => 1, 'value' => '恶意发起咨询'],
            ['key' => 2, 'value' => '骂人'],
        ];
        return success($data);
    }

    /**
     * 黑名单列表
     * @return bool|string
     */
    public function Index(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $where = [];
            if (!empty($param['name'])){
                $UserModel = new UserModel();
                $uids = $UserModel->where('name|username','like','%'.$param['name'].'%')->column('id');
                if (count($uids) > 0){
                    $where[] = ['uid','in',$uids];
                }else{
                    $where[] = ['id','=',-1];
                }
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['source'])){
                $where[] = ['source','in',$param['source']];
            }
            if (!empty($param['reason'])){
                $where[] = ['reason','in',$param['reason']];
            }
            if (!empty($param['duration'])){
                $where[] = ['duration','in',$param['duration']];
            }
            if (!empty($param['consult_id'])){
                $where[] = ['consult_id','in',$param['consult_id']];
            }
            if(!empty($param['class_id'])){
                $UserModel = new UserModel();
                $ids = $UserModel->where('department_id', 'in', $param['class_id'])->column('id');
                if (count($ids)) {
                    $where[] = ['uid', 'in', $ids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['id'])){
                $where[] = ['id', 'in', $param['id']];
            }
            $where[] = ['unit_id','=',$param['unit_id']];
            $ConsultBlacklistModel = new ConsultBlacklistModel();
            $query = $ConsultBlacklistModel->where($where);
            $data = $query->with([
                'user', 'consult'
            ])->order('id desc')->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            $UnitModel = new UnitModel();
            $data['unit_name'] = $UnitModel->where('id','=',$param['unit_id'])->value('name');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     *  黑名单高级筛选导出
     * @return bool|string
     */
    public function exportBlacklist(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $param['role_mode'] = 0;
            $param['user_id'] = 0;
            $param['blackout_duration'] = $this->blackoutDuration(); //拉黑时长
            $param['black_reason'] = $this->blackReason(); //拉黑原因
            $exportBlackService = new ExportBlackService($this->app);
            $ret = $exportBlackService->exportBlack($param);
            if (!$ret) throw new \Exception($exportBlackService->getMessage());
            return success($exportBlackService->getResult());
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

}